<?php
/**
 * Created by JetBrains PhpStorm.
 * User: joedarkangel3
 * Date: 8/27/11
 * Time: 12:54 AM
 * To change this template use File | Settings | File Templates.
 */
 
class validate_string_library_test extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('validation/validation_string');
        $this->load->library('unit_test');
        $this->unit->use_strict(true);
    }

    public function index()
    {
        // accept only string test, non-string will throw exception, it is consider programmer's fault
        $this->validation_string->set_data(234)->exact_length(3);
        $string_test = $this->validation_string->result();
        $this->unit->run(false, $string_test, "only accept string test");

        // invalid length parameter test
        $this->validation_string->set_data("234")->exact_length('3');
        $exact_length_test = $this->validation_string->result();
        $this->unit->run(false, $exact_length_test, "exact length test");

        // length test
        $this->validation_string->set_data("234")->exact_length(3);
        $exact_length_test = $this->validation_string->result();
        $this->unit->run(true, $exact_length_test, "exact length test");

        $this->validation_string->set_data("234")->exact_length(4);
        $iv_exact_length_test = $this->validation_string->result();
        $this->unit->run(false, $iv_exact_length_test, "iv exact length test");

        $this->validation_string->set_data("234")->min_length(3)->max_length(3);
        $min_max_length_test = $this->validation_string->result();
        $this->unit->run(true, $min_max_length_test, "v min, max length test");

        $this->validation_string->set_data("234")->min_length(4);
        $min_length_test = $this->validation_string->result();
        $this->unit->run(false, $min_length_test, "iv min length test");

        $this->validation_string->set_data("234")->max_length(2);
        $max_length_test = $this->validation_string->result();
        $this->unit->run(false, $max_length_test, "iv max length test");

        // natural number string test

        $this->validation_string->set_data("0234")->is_natural();
        $this->validation_string->set_data("2340")->is_natural();
        $v_natural_num_test = $this->validation_string->result();
        $this->unit->run(true, $v_natural_num_test, "valid natural number test");

        $this->validation_string->set_data("a234")->is_natural();
        $iv_natural_num_test = $this->validation_string->result();
        $this->unit->run(false, $iv_natural_num_test, "invalid natural number test");

        // zipcode test NOTE: not very good validation, need imporve it
        $this->validation_string->set_data("234-234")->is_us_zipcode();
        $this->validation_string->set_data("-233-4223")->is_us_zipcode();
        $v_zipcode_test = $this->validation_string->result();
        $this->unit->run(true, $v_zipcode_test, "valid zipcode test");

        $this->validation_string->set_data("a234")->is_us_zipcode();
        $iv_zipcode_test = $this->validation_string->result();
        $this->unit->run(false, $iv_zipcode_test, "invalid zipcode test");

        // zipcode test NOTE: not very good validation, need imporve it
        $this->validation_string->set_data("234-234")->is_sequence();
        $this->validation_string->set_data("233-4223")->is_sequence();
        $v_sequence_test = $this->validation_string->result();
        $this->unit->run(true, $v_sequence_test, "valid sequence test");

        $this->validation_string->set_data("a234")->is_sequence();
        $iv_sequence_test = $this->validation_string->result();
        $this->unit->run(false, $iv_sequence_test, "invalid sequence test");



        $this->_report();
    }

    private function _report()
    {
        if($this->input->is_ajax_request())
        {
            $final_result = $this->unit->result();
            $passed = 0;
            foreach($final_result as $res)
            {
                if($res['Result'] === "Passed")
                {
                    $passed++;
                }
            }
            if($passed == count($final_result))
            {
                $msg = json_encode(array('result' => 'passed'));
                echo $msg;
            }
            else{
                $msg = json_encode(array('result' => 'failed'));
                echo $msg;
            }
        }
        else{
            echo $this->unit->report();
        }
    }
}